/**
 * Octopus, an XMMS2 client.
 * Copyright (C) 2014 Octopus Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace Octopus {
	public class Configure : Object {
		private GLib.KeyFile _file;

		public Configure () {
			Object ();
		}

		construct {
			this._file = new GLib.KeyFile();
			read_config ();
		}

		private string build_filename () throws GLib.FileError {
			var path = GLib.Path.build_filename (Xmms.Client.userconfdir_get(),
			                                     "clients");

			if (GLib.FileUtils.test(path, GLib.FileTest.EXISTS)) {
				if (!GLib.FileUtils.test(path, GLib.FileTest.IS_DIR)) {
					throw new GLib.FileError.NOTDIR(path + " is not a directory");
				}
			} else {
				if (GLib.DirUtils.create_with_parents(path, 0755) < 0) {
					throw new GLib.FileError.FAILED("Failed to create " + path);
				}
			}

			return GLib.Path.build_filename(path, "octopus.conf");
		}

		private void read_config () {
			string filename;
			try {
				filename = build_filename();
				this._file.load_from_file(filename, GLib.KeyFileFlags.NONE);
			} catch (GLib.FileError e) {
				/* GLib.FileError.NOENT == 4, which is true the first time */
				if (e.code != 4) {
					GLib.warning ("error loading octopus.conf: %s", e.message);
				}
			} catch (GLib.KeyFileError e) {
				GLib.warning ("error loading octopus.conf: %s", e.message);
			}
		}

//GLib.KeyFile is a record, js does not seem to like records with methods.
//		public unowned GLib.KeyFile get_file () {
//			return this._file;
//		}

		public bool has_key (string group_name, string key) {
			return _file.has_key (group_name, key);
		}

		public int get_integer (string group_name, string key) {
			return _file.get_integer (group_name, key);
		}

		public void set_integer (string group_name, string key, int val) {
			_file.set_integer (group_name, key, val);
		}

		public string get_string (string group_name, string key) {
			return _file.get_string (group_name, key);
		}

		public void set_string (string group_name, string key, string val) {
			_file.set_string (group_name, key, val);
		}

		public void save () {
			GLib.FileStream stream;
			size_t length;

			try {
				string filename = build_filename();
				stream = GLib.FileStream.open(filename, "w");
				stream.puts(this._file.to_data(out length));
			} catch (GLib.FileError e) {
				GLib.warning ("error saving octopus.conf: %s", e.message);
			}
		}
	}
}
